package com.bss.dao;

import java.util.List;

import org.springframework.stereotype.Repository;
import org.springframework.transaction.annotation.Transactional;

import com.andy.basics.Dao;
import com.bss.model.Admission;
import com.bss.model.Receipt;

@Repository
public class ReceiptDao extends Dao<Receipt> {

	@Transactional
	public List<Receipt> findReceipts(final Admission admission) {

		admission.getRoomBookings().size();
		admission.getBedBookings().size();

		String commaList = this.createPayablesList(admission);

		List<Receipt> list = this.findComplex("Select distinct rcpts from " + Receipt.class.getName() + " as rcpts "
				+ "join rcpts.payments as pmnts " + "join pmnts.payable as pabl where pabl.id in (" + commaList
				+ " ) order by rcpts.date asc ");

		return list;

	}

	protected String createPayablesList(final Admission admission) {

		StringBuilder sb = new StringBuilder();

		this.getUtils().createCommaSeparatedIDs(admission.getRoomBookings(), sb);

		if (admission.getRoomBookings().size() > 0) {
			sb.append(',');
		}

		this.getUtils().createCommaSeparatedIDs(admission.getBedBookings(), sb);

		if (admission.getBedBookings().size() > 0) {
			sb.append(',');
		}

		// Few payments are directly for admission as Security Deposit, Utensil
		// Deposit etc
		sb.append(admission.getId());

		return sb.toString();
	}
}
